/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacenamiento;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author UNSAAC73
 */
public class Relacion implements java.io.Serializable, Iterable<Atributo> {
    private List<Atributo> atributos;
    
    public Relacion(){
        atributos=new ArrayList<Atributo>();
    }
    public Relacion(List<Atributo> atributos){
        this.atributos=new ArrayList<Atributo>(atributos);
    }
    protected void adicionarAtributo(Atributo atributo){
        atributos.add(atributo);
    }
    protected void EliminarAtributo(int indice){
               
        atributos.remove(indice);
    }
    public int getNumeroDeAtributos(){
        return atributos.size();
    }
    public Iterator<Atributo> iterator(){
        return atributos.iterator();
    }

    public List<Atributo> getAtributos() {
        return atributos;
    }
    public Atributo getAtributo(int i){
        return atributos.get(i);
    }
    
    public int getIndiceAtributo2(String atr){
        int i=-1;
        for(Atributo atributo: atributos){
            i++;
            if(atributo.getNombre().equals(atr)){
                return i;
            }
        }
        return -1;
    }
    
    public int getIndiceAtributo(String atr){
        int i=0;
        for(Atributo atributo: atributos){
            if(atributo.getNombre().equals(atr)){
                return i;
            }
        }
        return -1;
    }
    @Override
    public String toString(){
        StringBuilder sb=new StringBuilder();
        sb.append("{");
        for(int i=0;i<getNumeroDeAtributos()-1;i++){
            sb.append(getAtributo(i)).append(", ");
            
        }
        sb.append(getAtributo(getNumeroDeAtributos()-1)).append("} ");
        return sb.toString();
    }
}
